package cn.iocoder.yudao.module.cms.benefit.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.cms.benefit.dal.dataobject.BenefitDO;
import cn.iocoder.yudao.module.cms.benefit.service.BenefitService;
import cn.iocoder.yudao.module.cms.benefit.vo.BenefitPageReqVO;
import cn.iocoder.yudao.module.cms.benefit.vo.BenefitRespVO;
import cn.iocoder.yudao.module.cms.benefit.vo.BenefitSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 权益保障")
@RestController
@RequestMapping("/cms/benefit")
@Validated
public class BenefitController {

    @Resource
    private BenefitService benefitService;

    @PostMapping("/create")
    @Operation(summary = "创建权益保障")
    @PreAuthorize("@ss.hasPermission('cms:benefit:create')")
    public CommonResult<Long> createBenefit(@Valid @RequestBody BenefitSaveReqVO reqVO) {
        return success(benefitService.createBenefit(reqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新权益保障")
    @PreAuthorize("@ss.hasPermission('cms:benefit:update')")
    public CommonResult<Boolean> updateBenefit(@Valid @RequestBody BenefitSaveReqVO reqVO) {
        benefitService.updateBenefit(reqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除权益保障")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:benefit:delete')")
    public CommonResult<Boolean> deleteBenefit(@RequestParam("id") Long id) {
        benefitService.deleteBenefit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得权益保障")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:benefit:query')")
    public CommonResult<BenefitRespVO> getBenefit(@RequestParam("id") Long id) {
        BenefitDO benefit = benefitService.getBenefit(id);
        return success(BeanUtils.toBean(benefit, BenefitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得权益保障分页")
    @PreAuthorize("@ss.hasPermission('cms:benefit:query')")
    public CommonResult<PageResult<BenefitRespVO>> getBenefitPage(@Valid BenefitPageReqVO pageReqVO) {
        PageResult<BenefitDO> pageResult = benefitService.getBenefitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BenefitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出权益保障 Excel")
    @PreAuthorize("@ss.hasPermission('cms:benefit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBenefitExcel(@Valid BenefitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BenefitDO> list = benefitService.getBenefitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "权益保障.xls", "数据", BenefitRespVO.class,
                        BeanUtils.toBean(list, BenefitRespVO.class));
    }

}
